package com.galeno.day09.test;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/10/2619:46
 */
public class KeyedStreamUseKeyedState {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",12345);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        DataStreamSource<String> lines = env.socketTextStream("192.168.77.3", 9999);
        //如果开启了checkpoint,job默认的重启策略为无限重启(Integer.MaxValue)
        env.enableCheckpointing(10000);
        /**
         * spark,2
         * hive,1
         */
        SingleOutputStreamOperator<Tuple2<String, Integer>> tpStream = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] split = value.split(",");
                String word = split[0];
                int count = Integer.parseInt(split[1]);
                if (word.contains("error")) {
                    throw new RuntimeException("数据有误");
                }
                return Tuple2.of(word, count);


            }
        });
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = tpStream.keyBy(tp -> tp.f0);

        SingleOutputStreamOperator<Tuple2<String, Integer>> process = keyedStream.process(new KeyedProcessFunction<String, Tuple2<String, Integer>, Tuple2<String, Integer>>() {


            private transient ValueState<Integer> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                ValueStateDescriptor<Integer> valueStateDescriptor = new ValueStateDescriptor<>("count-state", Integer.class);
                valueState = getRuntimeContext().getState(valueStateDescriptor);
            }

            @Override
            public void processElement(Tuple2<String, Integer> in
                    , Context ctx
                    , Collector<Tuple2<String, Integer>> out) throws Exception {

                Integer current = in.f1;
                Integer history = valueState.value();
                if (history == null) {
                    history = 0;
                }
                current += history;
                valueState.update(current);
                in.f1 = current;
                out.collect(in);


            }
        });
        process.print();
        env.execute();


    }
}
